IOS 常用正则

在开发过程中,有时需要对用户输入的类型做判断,最常见是在注册页面即用户名和密码,直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma - mark 只能为中文
-(BOOL)onlyInputChineseCharacters:(NSString*)string{
NSString *inputString = @"[\u4e00-\u9fa5]+";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",inputString];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 只能为数字
- (BOOL)onlyInputTheNumber:(NSString*)string{
NSString *numString =@"[0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numString];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 只能为小写
- (BOOL)onlyInputLowercaseLetter:(NSString*)string{
NSString *regex =@"[a-z]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 只能为大写
- (BOOL)onlyInputACapital:(NSString*)string{
NSString *regex =@"[A-Z]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 允许大小写
- (BOOL)InputCapitalAndLowercaseLetter:(NSString*)string{
NSString *regex =@"[a-zA-Z]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 允许含大小写或数字(不限字数)
- (BOOL)inputLettersOrNumbers:(NSString*)string{
NSString *regex =@"[a-zA-Z0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 允许含大小写或数字(限字数)
-(BOOL)inputNumberOrLetters:(NSString*)name {
NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
BOOL inputString = [userNamePredicate evaluateWithObject:name];
return inputString;
}
#pragma - mark 允许汉字或数字(不限字数)
- (BOOL)inputChineseOrNumbers:(NSString*)string{
NSString *regex =@"[\u4e00-\u9fa5]+[0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 允许汉字或数字(限字数)
- (BOOL)inputChineseOrNumbersLimit:(NSString*)string{
NSString *regex =@"[\u4e00-\u9fa5][0-9]{6,20}+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 允许汉字,大小写或数字(不限字数)
- (BOOL)inputChineseOrLettersAndNumbersNum:(NSString*)string{
NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}
#pragma - mark 允许汉字,大小写或数字(限字数)
- (BOOL)inputChineseOrLettersNumberslimit:(NSString*)string{
NSString *regex =@"[\u4e00-\u9fa5]+[A-Za-z0-9]{6,20}+$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
BOOL inputString = [predicate evaluateWithObject:string];
return inputString;
}

转:http://blog.csdn.net/h643342713/article/details/53966446

Author

陈昭

Posted on

2017-01-03

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.